const api = require("../../utils/api");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isFirst:1,
    userNum:'',//当前队伍成员
    groupId:'',
    groupInfo:'',
    groupUser:'',
    groupImg:'',
    isMainUser:false,//当前用户是不是队长
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      groupId:options.groupId
    })
    this.getGroupUsers(options.groupId);
    this.getGroupDetail(options.groupId);
  },
   /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(this.data.isFirst >1){
      // this.getGroupUsers(this.data.groupId);
      this.getGroupDetail(this.data.groupId);
    }
    this.data.isFirst++;
  },
  // 查看当前用户是不是队长
  getUserStatus:function(mainId){
    let userInfo = wx.getStorageSync('userInfo');
    let id = userInfo._id;
    if(mainId == id){
      this.setData({
        isMainUser:true
      })
    }
  },
  getGroupDetail:function(id){
    api.post('group/getGroupDetail',{
      groupId:id
    }).then(res => {
      if(!!res.data){
        let {mainUser,maxNum,unjoinNum} = res.data;
        this.setData({
          userNum:maxNum-unjoinNum,
          groupInfo:res.data
        });
        wx.setStorage({
          data: res.data,
          key: 'groupInfo',
        })
        this.getUserStatus(mainUser);
        this.getGroupUsers(id,mainUser)
      }
    }).catch(err =>{
      console.log(err)
    })
  },
  getGroupUsers:function(id,mainid){
    api.post('group/getGroupUsers',{
      groupId:id
    }).then(res => {
      if(res.data.length>0){
        let arr = res.data;
        if(res.data.length>1){
          arr.forEach((item,index) =>{
            if(item._id == mainid){
              console.log(item)
              arr.splice(index,1);
              arr.unshift(item)
              console.log('arr',arr)
              this.setData({
                groupUser:arr
              })
            }
          })
        }else{
          this.setData({
            groupUser:res.data
          })
        }
        
        
        
      }
    }).catch(err =>{
      console.log(err)
    })
  },
  // 去编辑页
  toEidt:function(e){
    if(this.data.isMainUser){
      wx.navigateTo({
        url: './edit?type='+e.currentTarget.dataset.type+'&groupId='+this.data.groupId
      })
    }else{
      wx.showToast({
        title: '只有队长有权限修改',
        icon:'none'
      })
    }
  },
  // 去队伍问题列表
  toAQList:function(){
    wx.navigateTo({
      url: './aq-list'
    })
  },
  //添加图片
  addGroupImg: function() {
    if(this.data.isMainUser){
      wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success: (res) => {
          let that = this;
          const tempFilePaths = res.tempFilePaths[0]
           this.setData({
               coverImg: tempFilePaths
           });
           this.coverImg = tempFilePaths;
           wx.uploadFile({
               url: 'http://localhost:3000/group/addGroupImg', //服务器接口地址
               filePath: tempFilePaths,
               name: 'file',
                formData: {
                  'groupId': that.data.groupId
                },
               success: function (res) {
                that.setData({
                  groupImg:res.data
                })  
              },
               fail: function (e) {
                   console.log(e.stack);
                }
             })
          }
        })
    }else{
      wx.showToast({
        title: '只有队长有权限修改',
        icon:'none'
      })
    }
    
   
  },
  // 退出队伍
  exitGroup:function(){
    let token = wx.getStorageSync('token');
    let that = this;
    wx.showModal({
      title: '真的要退出吗？',
      content: '',
      showCancel: true,//是否显示取消按钮
      success: function (res) {
         if (res.cancel) {
          wx.showToast({
            title: '已取消',
            icon:'none'
          })
         } else {
          api.post('group/exitGroup',{
            token:token,
            groupId:that.data.groupId
          }).then(res => {
            wx.showToast({
              title: '退出成功',
            })
            setTimeout(function(){
              wx.switchTab({
                url: '/pages/index/index',
              })
            },500);
          }).catch(err => {
            console.log(err)
          })
         }
      },
      fail: function (res) {
        wx.showToast({
          title: '忙，稍后再试试吧',
          icon:'none'
        })
       }//接口调用失败的回调函数
   })
   
    
  },
  // 解散队伍
  deleteGroup:function(){
    let token = wx.getStorageSync('token');
    let that = this;
    wx.showModal({
      title: '真的要退出吗？',
      content: '',
      showCancel: true,//是否显示取消按钮
      success: function (res) {
         if (res.cancel) {
          wx.showToast({
            title: '已取消',
            icon:'none'
          })
         } else {
          api.post('group/deleteGroup',{
            token:token,
            groupId:that.data.groupId
          }).then(res => {
            wx.showToast({
              title: '队伍已解散',
            })
            setTimeout(function(){
              wx.switchTab({
                url: '/pages/index/index',
              })
            },500);
          }).catch(err => {
            console.log(err)
          })
         }
      },
      fail: function (res) {
        wx.showToast({
          title: '忙，稍后再试试吧',
          icon:'none'
        })
       }//接口调用失败的回调函数
   })
  }
})